package com.aier.classes.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.classes.entity.Classes;
import com.aier.common.common.SystemDateFormatUtil;

public class ClassesRowMapper implements RowMapper<Classes> {

    private int type;

    public static final int AND_TEACHERNAMES = 1;
//    
    public static final int AND_USERNUMS = 2;
    
    public static final int AND_TID = 3;

    public ClassesRowMapper() {
    }

    public ClassesRowMapper(int type) {
        this.type = type;
    }

    @Override
    public  Classes mapRow(ResultSet rs, int rowNum)throws SQLException {
        Classes c = parseClass(new Classes(), rs);
        switch (type) {
            case AND_TEACHERNAMES:
                c = parseClass(new Classes(), rs, "c");
                c.setTnames(rs.getString("tnames"));
                c.setCtime_str(SystemDateFormatUtil.formatDate(c.getCtime(), SystemDateFormatUtil.FORMAT_DATETIME_NOTSECOND));
                c.setNum_student(rs.getInt("num_student"));
                break;
            case AND_USERNUMS:
                c = parseClass(new Classes(), rs);
                c.setNum_teacher(rs.getInt("num_teacher"));
                c.setNum_student(rs.getInt("num_student"));
                c.setNum_parent(rs.getInt("num_parent"));
                break;
            case AND_TID:
                c = parseClass(new Classes(), rs, "c");
                c.setTid(rs.getLong("tid"));
            default:

        }
        return c;
    }

    public static Classes parseClass(Classes cl, ResultSet rs)
        throws SQLException {
        return parseClass(cl, rs, null);
    }
    
    public static Classes parseClass(Classes cl, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            cl.setCid(rs.getLong(su + "cid"));
        } catch (SQLException e) {
            cl.setCid(null);
        }

        try {
            cl.setCname(rs.getString(su + "cname"));
        } catch (SQLException e) {
            cl.setCname(null);
        }
        try {
            cl.setSid(rs.getInt(su + "sid"));
        } catch (SQLException e) {
            cl.setSid(null);
        }
        try {
            cl.setDesinfo(rs.getString(su + "desinfo"));
        } catch (SQLException e) {
            cl.setDesinfo(null);
        }
        try {
            cl.setManifesto(rs.getString(su + "manifesto"));
        } catch (SQLException e) {
            cl.setManifesto(null);
        }
        try {
            cl.setImg(rs.getString(su + "img"));
        } catch (SQLException e) {
            cl.setImg(null);
        }
        try {
            cl.setClogo(rs.getString(su + "clogo"));
        } catch (SQLException e) {
            cl.setClogo(null);
        }
        try {
            cl.setCtime(rs.getTimestamp(su + "ctime"));
        } catch (SQLException e) {
            cl.setCtime(null);
        }
        try {
            cl.setMesCount(rs.getInt(su + "mesCount"));
        } catch (SQLException e) {
            cl.setMesCount(null);
        }
        return cl;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, Classes cl) {
        par.put("cid", cl.getCid());
        par.put("cname", cl.getCname());
        par.put("sid", cl.getSid());
        par.put("desinfo", cl.getDesinfo());
        par.put("manifesto", cl.getManifesto());
        par.put("img", cl.getImg());
        par.put("clogo", cl.getClogo());
        par.put("ctime", cl.getCtime());
        par.put("mesCount", cl.getMesCount());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            Classes cl) {
        args.put("cid", cl.getCid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final Classes cl) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(cl),
                        new String[] { "cid" });
                parsePS(ps, cl);
                return ps;
            }

        };
    }

    private static String parseSQL(Classes cl) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into classes(");
        int count = 0;
        if (null != cl.getCname()) {
            sql.append("cname,");
            count++;
        }

        if (null != cl.getSid()) {
            sql.append("sid,");
            count++;
        }

        if (null != cl.getDesinfo()) {
            sql.append("desinfo,");
            count++;
        }

        if (null != cl.getManifesto()) {
            sql.append("manifesto,");
            count++;
        }
        if (null != cl.getImg()) {
            sql.append("img,");
            count++;
        }
        
        if (null != cl.getClogo()) {
            sql.append("clogo,");
            count++;
        }
        if (null != cl.getCtime()) {
            sql.append("ctime,");
            count++;
        }
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, Classes cl)
        throws SQLException {
        int count = 1;
        if (null != cl.getCname()) {
            ps.setString(count++, cl.getCname());
        }
        if (null != cl.getSid()) {
            ps.setInt(count++, cl.getSid());
        }
        if (null != cl.getDesinfo()) {
            ps.setString(count++, cl.getDesinfo());
        }
        if (null != cl.getManifesto()) {
            ps.setString(count++, cl.getManifesto());
        }
        if (null != cl.getImg()) {
            ps.setString(count++, cl.getImg());
        }
        if (null != cl.getClogo()) {
            ps.setString(count++, cl.getClogo());
        }
        if (null != cl.getCtime()) {
            ps.setTimestamp(count++, cl.getCtime());
        }
    }

}
